package com.stream;

import java.util.concurrent.CompletableFuture;

/**
 * @Author: 许磊磊
 * @Date: 2022/10/17 19:32
 * @Version 1.0
 */
public class ComplatableFutureTest {

    public static void main(String[] args) throws Exception {
//        CompletableFuture.runAsync(()->{
//            System.err.println(Thread.currentThread().getName());
//        });

//        System.err.println(CompletableFuture.supplyAsync(() -> {
//            try {
//                Thread.sleep(10000);
//            } catch (InterruptedException e) {
//                throw new RuntimeException(e);
//            }
//            return "hahahahahahahaha";
//        }).get());
//        CompletableFuture.supplyAsync(() -> {
//            try {
//                Thread.sleep(10000);
//            } catch (InterruptedException e) {
//                throw new RuntimeException(e);
//            }
//            System.err.println("线程执行了");
//            return "hahahahahahahaha";
//        });
//
//        System.err.println("aasdasdasdasd");
        CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            return "nihao";
        }).thenAccept(System.err::println);//then就是利用上面的返回结果来继续后面的异步事情

        System.err.println("aasdasdasdasd");
        System.in.read();
    }
}
//非核心业务,有中间状态可以临时保存的都可以使用异步
//同步变成很容易导致异常传递
//故障隔离, 熔断, 用多线程异步
